/*
 * Copyright (C) 1997-2020 康成投资（中国）有限公司
 *
 * http://www.rt-mart.com
 *
 * 版权归本公司所有，不得私自使用、拷贝、修改、删除，否则视为侵权
 */
/*
 * com.zw.admin.server.component.impl
 * @author xiaoqun.liu
 * @date 2021年5月12日 上午9:42:10
 */
package com.zw.admin.server.component.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zw.admin.server.component.PurchOrderComponent;
import com.zw.admin.server.dao.PPurchOrderItemsDao;
import com.zw.admin.server.dao.PPurchOrdersDao;
import com.zw.admin.server.model.PPurchOrderItems;
import com.zw.admin.server.model.PPurchOrders;

/**
 *
 * @author xiaoqun.liu
 * @date 2021年5月12日 上午9:42:10
 */
@Component
public class PurchOrderComponentImpl implements PurchOrderComponent {

	@Autowired
	private PPurchOrdersDao pPurchOrdersDao;

	@Autowired
	private PPurchOrderItemsDao pPurchOrderItemsDao;
	
	/**
	 * 更新采购单 状态 
	 * @param orderId
	 * @param status
	 * @return
	 * @author xiaoqun.liu
	 * @date 2021年5月12日 上午9:44:00
	 */
	public Integer updatePurchOrderStatus(Long orderId,Integer status) {
		PPurchOrders sPPurchOrders = pPurchOrdersDao.getById(orderId);
		if(sPPurchOrders==null) {
			return  -1;
		}
		if(status==2 && sPPurchOrders.getStatus()<=2 ) {
			PPurchOrders pPurchOrders= new PPurchOrders();
			pPurchOrders.setId(orderId);
			pPurchOrders.setStatus(status);

			culcalPPurchOrders(pPurchOrders);
			
			pPurchOrdersDao.update(pPurchOrders);
			return 1;
		}else if(status==2 && sPPurchOrders.getStatus()<=1) {
			PPurchOrders pPurchOrders= new PPurchOrders();
			pPurchOrders.setId(orderId);
			pPurchOrders.setStatus(status);
			
			culcalPPurchOrders(pPurchOrders);
			pPurchOrdersDao.update(pPurchOrders);
			return 1;
		}
		
		return 0;
	}
	
	public void culcalPPurchOrders(PPurchOrders pPurchOrders) {
		if(pPurchOrders==null) return ;
		List<PPurchOrderItems> listItems  = pPurchOrderItemsDao.listOrderId(pPurchOrders.getId());
		BigDecimal totalAmout = new BigDecimal(0);

		for(PPurchOrderItems pPurchOrderItems : listItems) {
			totalAmout=totalAmout.add( pPurchOrderItems.getPreBuyPrice().multiply(pPurchOrderItems.getQty() ) );
			pPurchOrderItems.getPreBuyPrice() ;
			pPurchOrderItems.getQty() ;
			
		}
		pPurchOrders.setAmount(totalAmout );
		pPurchOrders.setPreAmount(totalAmout);
		pPurchOrders.setPreGoodsAmount(totalAmout);
	}


	@Override
	public Integer saveCulcalPPurchOrders(Long orderId) {
		PPurchOrders pPurchOrders= new PPurchOrders();
		pPurchOrders.setId(orderId);
		culcalPPurchOrders(pPurchOrders);
		return pPurchOrdersDao.update(pPurchOrders);
	}
	
	@Override
	public void fixPurchOrderStatusTitle(PPurchOrders pPurchOrders) {
		if(pPurchOrders==null) return ;
		String statusTitle= null;
		switch (pPurchOrders.getStatus()) {
			case 0: statusTitle="待下单";break;
			case 1: statusTitle="下单";break;
			case 2: statusTitle="向供应商下单";break;
			case 3: statusTitle="供应商发货";break;
			case 4: statusTitle="取消下单";break;
			case 5: statusTitle="部分收货";break;
			case 6: statusTitle="退货";break;
			case 7: statusTitle="全部收货";break;
			case 8: statusTitle="结算完结";break;
		}
		if(statusTitle!=null) {
			pPurchOrders.setStatusTitle(statusTitle);
		}
	}
}
